package com.cn.springcloudwechat.controller;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.base.Const;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.model.*;
import com.cn.springcloudwechat.model.articleAndMenu.YmArticle;
import com.cn.springcloudwechat.model.attachment.YmAttachment;
import com.cn.springcloudwechat.model.attachment.YmAttachmentRelevance;
import com.cn.springcloudwechat.model.dto.ArticleDetailsDto;
import com.cn.springcloudwechat.model.dto.ArticleDto;
import com.cn.springcloudwechat.model.find.communication.YmAnswerVO;
import com.cn.springcloudwechat.model.find.communication.YmExchange;
import com.cn.springcloudwechat.model.find.communication.YmExchangeVO;
import com.cn.springcloudwechat.service.YmAnswerService;
import com.cn.springcloudwechat.service.YmUserInfromService;
import com.cn.springcloudwechat.service.find.communication.CommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/web/ymUserInfrom/")
public class YmUserInfromController {

    @Autowired
    private YmUserInfromService ymUserInfromService;

    @Autowired
    private CommunicationService communicationService;

    @Autowired
    private YmAnswerService ymAnswerService;

    @RequestMapping("information")
    @LogInfo(content = "系统消息", operateType = OperateType.QUERY)
    public AjaxRes getInformation(AjaxRes ajaxRes){
        try{
            YmUser user= ThreadLocalUtil.getUser();
            Long userId=56l;
            List<Map<String,Object>> list=ymUserInfromService.getInformation(user.getUserId());
            ajaxRes.setSucceed(list, Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("remind")
    @LogInfo(content = "服务提醒", operateType = OperateType.QUERY)
    public AjaxRes getRemind(AjaxRes ajaxRes){
        try{
            YmUser user= ThreadLocalUtil.getUser();
            Long userId=56l;
            List<Map<String,Object>> list=ymUserInfromService.getRemind(user.getUserId());

            ajaxRes.setSucceed(list, Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("order")
    @LogInfo(content = "用户订单", operateType = OperateType.QUERY)
    public AjaxRes getYmOrder(AjaxRes ajaxRes,String orderPayState){
        try{
           YmUser user= ThreadLocalUtil.getUser();
            Long userId=56l;
            List<Map<String,Object>> list=ymUserInfromService.getYmOrder(user.getUserId(),orderPayState);

            if(list.size() != 0){
                ajaxRes.setSucceed(list,Const.DATA_SUCCEED);
            }
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }



    @RequestMapping("exchange")
    @LogInfo(content = "用户交流圈帖子", operateType = OperateType.QUERY)
    public AjaxRes getYmExchange(AjaxRes ajaxRes){
        List<Map<String,Object>> list = new ArrayList<>();
        try{
            YmUser user= ThreadLocalUtil.getUser();
            Map<String,Object> map = new HashMap<>();
            Long userId=56l;
            List<Map<String,Object>> list1=ymUserInfromService.getYmExchange(user.getUserId());
            map.put("list1",list1);
            map.put("no",list1.size());
            list.add(map);
            ajaxRes.setSucceed(list,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("exchangeDetails")
    @LogInfo(content = "用户交流圈帖子详情", operateType = OperateType.QUERY)
    public AjaxRes getExchangeDetails(AjaxRes ajaxRes,Long exchangeId){
             List<Map<String,Object>> list = new ArrayList<>();
        try{
             YmUser user= ThreadLocalUtil.getUser();
            Long userId=56l;
            Map<String,Object> map = new HashMap<>();
            //帖子内容
            YmExchangeVO ymExchangeVO = communicationService.selectByPrimaryKey(exchangeId);
            //帖子回复内容
            List<YmAnswerVO> ymAnswers = communicationService.queryYmAnswerDetail(exchangeId,2);
            //点赞数
            YmPraise ymPraise = new YmPraise();
            ymPraise.setPraiseTypeId(exchangeId);
            ymPraise.setPraiseType("3");
            ymPraise.setUserId(user.getUserId());
            Integer count = communicationService.praiseCount(ymPraise);
            Integer state=communicationService.praiseExist(ymPraise);
            //附件
            YmAttachmentRelevance ymAttachmentRelevance = new YmAttachmentRelevance();
            ymAttachmentRelevance.setRelevanceId(exchangeId);
            ymAttachmentRelevance.setRelevanceTpyeId("3");
            List<YmAttachment> ymAttachments = communicationService.selectByRelevanceIdAndType(ymAttachmentRelevance);
            map.put("exchange",ymExchangeVO);
            map.put("ymAnswers",ymAnswers);
            map.put("praise",count);
            map.put("answersCount",ymAnswers.size());
            map.put("praiseState",state);
            map.put("ymAttachments",ymAttachments);
            list.add(map);
            ajaxRes.setSucceed(list,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("State")
    @LogInfo(content = "读取状态更改(系统消息，服务提醒)", operateType = OperateType.UPDATE)
    public AjaxRes updateState(AjaxRes ajaxRes,Long informId){
        try{
            Integer integer=ymUserInfromService.updateState(informId);
            ajaxRes.setSucceed(integer,Const.UPDATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }


    @RequestMapping("readState")
    @LogInfo(content = "读取状态更改（互动消息）", operateType = OperateType.UPDATE)
    public AjaxRes updateReadState(AjaxRes ajaxRes,Long answerId){
        try{
            Integer integer=ymUserInfromService.updateReadState(answerId);
            ajaxRes.setSucceed(integer,Const.UPDATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("interactive")
    @LogInfo(content = "互动消息", operateType = OperateType.QUERY)
    public AjaxRes Interactive(AjaxRes ajaxRes){
        List<Map<String,Object>> list = new ArrayList<>();
        try{
            YmUser user= ThreadLocalUtil.getUser();
            Long userId=56l;
            Map<String,Object> map = new HashMap<>();
            List<Map<String,Object>> list1=ymUserInfromService.getInteractiveMessage(user.getUserId());
            List<Map<String,Object>> list2=ymUserInfromService.getArticleReply(user.getUserId());
            List<Map<String,Object>> list3=ymUserInfromService.getDoctorAnswer(user.getUserId());
            List<Map<String,Object>> list4=ymUserInfromService.getExchangeAnswer(user.getUserId());
            List<Map<String,Object>> list5=ymUserInfromService.getDoctorAnswerUser(user.getUserId());
            map.put("list1",list1);
            map.put("list2",list2);
            map.put("list3",list3);
            map.put("list4",list4);
            map.put("list5",list5);
            list.add(map);
          ajaxRes.setSucceed(map,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("deleteOrder")
    @LogInfo(content = "订单删除", operateType = OperateType.DELETE)
    public AjaxRes deleteOrder(AjaxRes ajaxRes,Long orderId){
        try{
            YmUser user= ThreadLocalUtil.getUser();
            Long userId=56l;
            Integer integer=ymUserInfromService.deleteOrder(user.getUserId(),orderId);
            ajaxRes.setSucceed(integer,Const.DEL_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 获取关于我们文章id标题
     * @param ajaxRes
     * @return
     */
    @RequestMapping("getArticleDto")
    @LogInfo(content = "获取关于我们文章id标题", operateType = OperateType.QUERY)
    public AjaxRes getArticleDto(AjaxRes ajaxRes){
        try{
            List<ArticleDto> articleDtoList = ymUserInfromService.getArticleDto();
            ajaxRes.setSucceed(articleDtoList,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return  ajaxRes;
    }

    /**
     * 查询关于我们文章详情
     * @param ajaxRes
     * @param articleId
     * @return
     */
    @RequestMapping("getArticleDetails")
    @LogInfo(content = "查询关于我们文章详情", operateType = OperateType.QUERY)
    public AjaxRes getArticleDetails(AjaxRes ajaxRes,Long articleId){
        try{

            ArticleDetailsDto articleDetailsDto = ymUserInfromService.getArticleDetailsDto(articleId);
            ajaxRes.setSucceed(articleDetailsDto,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 互动回复
     * @param ajaxRes
     * @param ymAnswer
     * @return
     */
    @RequestMapping("interactiveResponse")
    @LogInfo(content = "互动消息的回复", operateType = OperateType.INSERT)
    public String insertConsult(AjaxRes ajaxRes, YmAnswer ymAnswer,int type){
        try {
            Integer integer = communicationService.insertAnser(ymAnswer,type);
            ajaxRes.setSucceed(integer, com.cn.springcloudwechat.common.utils.Const.CONFIGURATION_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes.toJSONString();
    }



}
